package com.gameley.dmgtpf.kernel.service.impl;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gameley.dmgtpf.common.exception.MyCustomException;
import com.gameley.dmgtpf.common.exception.MyRuntimeException;
import com.gameley.dmgtpf.common.utils.LoggerUtil;
import com.gameley.dmgtpf.common.utils.PubEnvUtil;
import com.gameley.dmgtpf.common.utils.RandomUtil;
import com.gameley.dmgtpf.common.utils.StreamUtil;
import com.gameley.dmgtpf.kernel.dao.GamelabelDao;
import com.gameley.dmgtpf.kernel.entity.GamelabelEntity;
import com.gameley.dmgtpf.kernel.service.GamelabelService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Service("gamelabelService")
public class GamelabelServiceImpl extends ServiceImpl<GamelabelDao, GamelabelEntity> implements GamelabelService
{
    private Lock lock = new ReentrantLock();
    private Condition c1 = lock.newCondition();
    
    @Autowired(required = true)
    private GamelabelDao gamelabelDao;
    static Map<String, String> map = new HashMap<String, String>();
    
    @Override
    public String file2tableSavaDB(MultipartFile jsonFile)
    {
        try
        {
            if (PubEnvUtil.isEmptyObj(jsonFile))
            {
                throw new MyRuntimeException("操作失败，文件不能为空！");
            }
            String str = StreamUtil.InputStreamTOString(jsonFile.getInputStream());
            if (PubEnvUtil.isEmpty(str))
            {
                throw new MyRuntimeException("操作失败，文件内容不能为空！");
            }
            
            GamelabelEntity entity = null;
            String strs[] = str.split(PubEnvUtil.CRLF);
            for (int i = 0; i < PubEnvUtil.getSize(strs); i++)
            {
                entity = new GamelabelEntity();
                String name = strs[i];
                if (PubEnvUtil.isEmpty(map.get(name)))
                {
                    TableName table = (TableName) entity.getClass().getAnnotation(TableName.class);
                    String tableName = table.value();
                    Integer id = gamelabelDao.selectIdAdd(tableName);
                    entity.setCode(RandomUtil.toFixdLengthString(id, 6));
                    entity.setName(name);
                    entity.setId(i);
                    entity.setType(-1);
                    
                    map.put(name, name);
                    PubEnvUtil.setAttributeValue(entity);
                    gamelabelDao.insert(entity);
                }
            }
            
        }
        catch (Exception e)
        {
            LoggerUtil.getInstance().exception(e);
            System.err.println(e);
        }
        
        return PubEnvUtil.getSuccessMsg("操作成功！！");
    }
    
    @Override
    public Boolean saveLabel(GamelabelEntity entity) throws Exception
    {
        
        lock.lock();
        try
        {
            PubEnvUtil.setAttributeValue(entity);
            if (PubEnvUtil.isEmptyObj(entity.getPid()))
            {
                entity.setPid(-1);
            }
            if (PubEnvUtil.isEmptyObj(entity.getType()))
            {
                entity.setType(-1);
            }
            
            // 主鍵自动生产
            // userMap.remove("id");
            TableName table = (TableName) entity.getClass().getAnnotation(TableName.class);
            String tableName = table.value();
            int count = gamelabelDao.insert(entity);
            return PubEnvUtil.equals(0, count) ? Boolean.FALSE : Boolean.TRUE;
        }
        catch (Exception e)
        {
            LoggerUtil.getInstance().exception(e);
            throw new MyCustomException(e);
        }
        finally
        {
            lock.unlock();
        }
    }
    
}
